package com.hethongviet.job.helpers;

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation
{
	private Timer timer;
	private LocationManager locationManager;
	private LocationResult locationResult;
	private boolean isGPSEnabled = false;
	private boolean isNetworkEnabled = false;
	private int durationLoopTime;

	public MyLocation()
	{
		durationLoopTime = 20000;
	}

	public MyLocation(int timer)
	{
		this.durationLoopTime = timer;
	}

	public boolean getLocation(Context context, LocationResult result,
			boolean isNetworkEnable)
	{
		// I use LocationResult callback class to pass location value from
		// MyLocation to user code.
		locationResult = result;
		if (locationManager == null) locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		// exceptions will be thrown if provider is not permitted.
		try
		{
			isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
		if (isNetworkEnable)
		{
			try
			{
				isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			} catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
		// don't start listeners if no provider is enabled
		if (!isGPSEnabled && !isNetworkEnabled) return false;

		if (isGPSEnabled) locationManager.requestLocationUpdates(
				LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
		if (isNetworkEnabled) locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

		timer = new Timer();
		timer.scheduleAtFixedRate(new GetLastLocation(), durationLoopTime,
				durationLoopTime);
		// timer.schedule(new GetLastLocation(), durationLoopTime);

		return true;
	}

	private LocationListener locationListenerGps = new LocationListener() {
		public void onLocationChanged(Location location)
		{
			//AppMain.debug("GPS location change!!!!");

			//timer.cancel();
			locationResult.gotLocation(location);
			// locationManager.removeUpdates(this);
			// locationManager.removeUpdates(locationListenerNetwork);
		}

		public void onProviderDisabled(String provider)
		{
		}

		public void onProviderEnabled(String provider)
		{
		}

		public void onStatusChanged(String provider, int status, Bundle extras)
		{
		}
	};

	private LocationListener locationListenerNetwork = new LocationListener() {
		public void onLocationChanged(Location location)
		{
			//AppMain.debug("GPS internet change!!!!");

			//timer.cancel();
			locationResult.gotLocation(location);
			// locationManager.removeUpdates(this);
			// locationManager.removeUpdates(locationListenerGps);
		}

		public void onProviderDisabled(String provider)
		{
		}

		public void onProviderEnabled(String provider)
		{
		}

		public void onStatusChanged(String provider, int status, Bundle extras)
		{
		}
	};

	private class GetLastLocation extends TimerTask
	{
		@Override
		public void run()
		{
			//AppMain.debug("GPS get last change!!!!");
			// locationManager.removeUpdates(locationListenerGps);
			// locationManager.removeUpdates(locationListenerNetwork);

			Location net_loc = null, gps_loc = null;
			if (isGPSEnabled) gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			if (isNetworkEnabled) net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

			// if there are both values use the latest one
			// if (gps_loc != null && net_loc != null)
			// {
			// AppMain.debug("GPS get last change1111!!!!");
			// if (gps_loc.getTime() > net_loc.getTime())
			// locationResult.gotLocation(gps_loc);
			// else locationResult.gotLocation(net_loc);
			// }
			// else if (gps_loc != null)
			// {
			// AppMain.debug("GPS get last change2222!!!!");
			// locationResult.gotLocation(gps_loc);
			// }
			// else if (net_loc != null)
			// {
			// AppMain.debug("GPS get last change33333!!!!");
			// locationResult.gotLocation(net_loc);
			// }
			// else
			// {
			// AppMain.debug("GPS get last change444444!!!!");
			// locationResult.gotLocation(null);
			// }

			if (net_loc != null)
			{
				//AppMain.debug("GPS get last change33333!!!!");
				locationResult.gotLocation(net_loc);
			}

			if (gps_loc != null)
			{
				//AppMain.debug("GPS get last change2222!!!!");
				locationResult.gotLocation(gps_loc);
			}
		}
	}

	// public void stop()
	// {
	// if (timer != null) timer.cancel();
	//
	// locationManager.removeUpdates(locationListenerGps);
	// locationManager.removeUpdates(locationListenerNetwork);
	// }

	public boolean isGPSEnable()
	{
		return isGPSEnabled;
	}

	public static abstract class LocationResult
	{
		public abstract void gotLocation(Location location);
	}
}
